/*
 * @Author: szx
 * @Date: 2022-02-17 14:01:06
 * @LastEditTime: 2022-02-17 14:19:12
 * @Description:
 * @FilePath: \leetcode\2000-2099\2013\2013.js
 */
var DetectSquares = function () {
    this.cnt = new Map();
};

/**
 * @param {number[]} point
 * @return {void}
 */
DetectSquares.prototype.add = function (point) {
    const x = point[0],
        y = point[1];
    if (!this.cnt.has(x)) {
        this.cnt.set(x, new Map());
    }
    const xCnt = this.cnt.get(x);
    xCnt.set(y, (xCnt.get(y) || 0) + 1);
};

/**
 * @param {number[]} point
 * @return {number}
 */
DetectSquares.prototype.count = function (point) {
    let res = 0;
    let x = point[0], y = point[1];
    if (!this.cnt.has(x)) {
        return 0;
    }
    const xCnt = this.cnt.get(x);
    const entries = this.cnt.entries();
    for (const [row, rowCnt] of entries) {
        if (row !== x) {
            // 根据对称性，这里可以不用取绝对值
            let d = row - x;
            res += (rowCnt.get(y) || 0) * (xCnt.get(y + d) || 0) * (rowCnt.get(y + d) || 0);
            res += (rowCnt.get(y) || 0) * (xCnt.get(y - d) || 0) * (rowCnt.get(y - d) || 0);
        }
    }
    return res;
};

/**
 * Your DetectSquares object will be instantiated and called as such:
 * var obj = new DetectSquares()
 * obj.add(point)
 * var param_2 = obj.count(point)
 */
